package com.md.youyouhui.ui.login;

import android.databinding.DataBindingUtil;
import android.widget.Toast;

import com.md.youyouhui.R;
import com.md.youyouhui.databinding.ActivityForgetPassTwoBinding;
import com.md.youyouhui.ui.BaseActivity;
import com.md.youyouhui.utils.ToastUtils;
import com.md.youyouhui.view.MainViewModel;

/**
 * Created by AdministratorZJ
 * on 2019/4/3.
 *
 * @author AdministratorZJ
 */
public class ForgetTwoActivity extends BaseActivity {
    private ActivityForgetPassTwoBinding twoBinding;
    private MainViewModel viewModel;

    @Override
    public void setContentLayout() {
        twoBinding = DataBindingUtil.setContentView(this, R.layout.activity_forget_pass_two);
        viewModel = new MainViewModel();
    }

    @Override
    public void initView() {
        setSupportActionBar(twoBinding.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public void initData() {
        String phone = getIntent().getStringExtra("phone");
        String code = getIntent().getStringExtra("code");
        twoBinding.confirmBtn.setOnClickListener(v -> forgotService(phone, code));
    }

    private void forgotService(String phone, String code) {
        String pass = twoBinding.forgetNewPass.getText().toString();
        String newPass = twoBinding.againNewPass.getText().toString();
        if (!pass.equals(newPass)) {
            Toast.makeText(ct, "密码不一致", Toast.LENGTH_SHORT).show();
            return;
        }
        viewModel.forgot(phone, code, newPass).observe(this, objectResource -> {
            assert objectResource != null;
            switch (objectResource.status) {
                case LOADING:
                    break;
                case SUCCESS:
                    Toast.makeText(ct, "修改成功", Toast.LENGTH_SHORT).show();
                    setResult(RESULT_OK);
                    finish();
                    break;
                case ERROR:
                    ToastUtils.errMake(this, objectResource.errorCode);
                    break;
                default:
                    break;
            }
        });
    }
}
